<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28 0028
 * Time: 下午 5:12
 */
namespace app\admin\controller;

use think\Db;
use think\Request;

class Navigation extends Base{



    public function _initialize() {
        parent::_initialize();
        $this->searchFields = [
            'type' => [
                'label'     => '位置',
                'field'     => 'type',
                'type'      => 'select',
                'disabled'  => false,
                'condition' => '=',
                'data' => [
                    '1' => '首页',
                    '2' => '党建'
                ]
            ],
        ];

        $this->redirect = 'admin/navigation/index';
        $this->defaultOrder = 'sorting ASC';
    }
    public function index(){
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();
            $model = model($this->model);
            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

            $where = $this->getFilterWhere($request);
            if(!$where){
                $where['type'] = 1;
            }
            if($this->defaultWhere){
                //$model = $model->where($this->defaultWhere);
                $where = array_merge($where, $this->defaultWhere);
            }
            /*if($this->defaultOrder){
                $model = $model->order($this->defaultOrder);
            }*/

            $list = $model->where($where)->limit($start, $length)->order($order)->select();
            $lists = $model->where($where)->select();
            $count = count($lists);
            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }
        return parent::index();
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return $this->error('参数错误');
        }
        $list = \app\admin\model\Navigation::get($param['id']);

        return $this->fetch('',[
            'data' => $list,
        ]);
    }

    public function editPost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(empty($param['id'])){
                return $this->error('缺少参数');
            }
            $Navigation = \app\admin\model\Navigation::get($param['id']);
            $Navigation->imgUrl = $param['imgUrl'];
            $Navigation->status = $param['status'];
            $Navigation->sort = $param['sort'];
            $Navigation->save();
            return $this->success('ok',$this->redirect);
        }else{
            return $this->error('非法请求');
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['type']) && $filter['type']){
                $where['type'] = $filter['type'];
            }
        }
        return $where;
    }

}